﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ebclang.basics;
using myapp.messages.Stopuhr;
using myapp.specifications;

namespace myapp.stopuhr
{
    public class Stopuhr : IStopuhr
    {
        private Timer timer = new Timer(x => { }, null, Timeout.Infinite, Timeout.Infinite);
        private DateTime startzeit, stopzeit;


        public void In_Start(Start message)
        {
            In_Stop(new Stop());

            startzeit = DateTime.Now;
            timer = new Timer(x => this.Out_Zeitfortschritt(DateTime.Now.Subtract(startzeit)), null, 0, 1000);
        }


        public void In_Stop(Stop message)
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            stopzeit = DateTime.Now;
        }


        public void In_GestoppteZeit(Request<GestoppteZeit, TimeSpan> request)
        {
            request.ResponsePin(stopzeit.Subtract(startzeit));
        }

        
        public event Action<TimeSpan> Out_Zeitfortschritt;
    }
}
